/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.gui.rendering;

import java.util.List;

import net.groupme.controller.ExportController;
import net.groupme.controller.model.HtmlFormattedElement;
import net.groupme.gui.constants.VIEW_VOCABULARY;
import net.groupme.model.GameResourceVisualizationImpl;
import net.groupme.model.Resource;
import net.groupme.model.ResourceVisualization;

/**
 * created on Jun 29, 2007
 * 
 * Abstract implementation of a {@link Renderer}. It equips subclasses with some
 * helping methods for visualizing e.g. the menu of a visualized {@link Resource}.
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: mischa $
 * 
 * @version $Revision: 1.30 $ $Date: 2008-06-01 20:27:43 $
 */
public abstract class AbstractRenderer implements Renderer {
	
	/**
	 * Returns the menu for a given {@link ResourceVisualization} instance.
	 * @param resourceVisualization the {@link ResourceVisualization} which will be rendered
	 * @param divID the ID of the actual content (without menu)
	 * @return the HTML-formatted menu for the given {@link ResourceVisualization} instance.
	 */
	public String getMenu(ResourceVisualization resourceVisualization, String divID, Integer groupWidth){
		String menu= "";
		if (resourceVisualization instanceof GameResourceVisualizationImpl){
				//open div_handel
			menu = "<div id=\"menuRes\" style=\"width" + (((resourceVisualization.getWidth()*groupWidth)/1000))
				+ "px; cursor:move;\" class=\"menuResourceOut\" id=\"" + divID + "_handle\""
				+ " onmousedown=\"mouseDownOnResVisu('"+ resourceVisualization.getURI().toString() + "')\">" +
				"<div class=\"menuResourceIn\" >" +
				"<a  href=\"javascript:addTag('" + resourceVisualization.getResource().getId() + "', '" + resourceVisualization.getURI().toString() + "')\" onfocus=\"if(this.blur)this.blur()\" title=\"add a Tag to the Resource\">[add Tag]</a>" +
				"<a style=\"cursor:default;\" href=\"javascript:removeResourceFromGroup('" + resourceVisualization.getURI().toString() + "')\" onfocus=\"if(this.blur)this.blur()\">[X]</a>" +
				" </div>" +
				//close div _handle
				" </div>";
		}
		else {
				//open div_handel	
			menu = "<div id=\"menuRes\" style=\"width" + (((resourceVisualization.getWidth()*groupWidth)/1000))
				+ "px; cursor:move;\" class=\"menuResourceOut\" id=\"" + divID + "_handle\""
				+ " onmousedown=\"mouseDownOnResVisu('"+ resourceVisualization.getURI().toString() + "')\">" +
				"<div class=\"menuResourceIn\" >" +
				"<a href=\"javascript:toggleTags('" + divID + "');\" onfocus=\"if(this.blur)this.blur()\" >[tags]</a>" +
				"<a  href=\"javascript:addTag('" + resourceVisualization.getResource().getId() + "', '" + resourceVisualization.getURI().toString() + "')\" onfocus=\"if(this.blur)this.blur()\" title=\"add a Tag to the Resource\">[add Tag]</a>" +
				// "<a onclick=\"addTag('" + resourceVisualization.getResource().getId() + "', '" + divID + "');\">[T+]</a>" +
				"<a href=\"javascript:showBigResource('" + resourceVisualization.getURI().toString() + "')\" onfocus=\"if(this.blur)this.blur()\">[menu]</a>" +
				"<a  href=\"" + resourceVisualization.getResource().getURL() + "\" target=\"_blank\" title=\"Open this Resource a in new window\">[open]</a>" + 
				"<a style=\"cursor:default;\" href=\"javascript:removeResourceFromGroup('" + resourceVisualization.getURI().toString() + "')\" onfocus=\"if(this.blur)this.blur()\">[X]</a>" +
				" </div>" +
				//close div _handle
				" </div>";
		}
		return menu;
	}
	
	/**
	 * Return the start div-Tag with all attributes.
	 * @param resourceVisualization the {@link ResourceVisualization} which will be rendered
	 * @param groupWidth the width of the group in which the resourceVisualization is grouped
	 * @param groupHeight the height of the group in which the resourceVisualization is grouped
	 * @return the HTMLformatted opening div-tag for the resource
	 */
	public String getDivAttributes(ResourceVisualization resourceVisualization, Integer groupWidth, Integer groupHeight, Integer zAxis){
		//div-open _border
		String div = "<div id=\"" + resourceVisualization.getURI().toString() + "_border\" class=\"ResourceVisualization\" style=\"min-width:160px; min-height:100px; position:absolute; border: 2px solid blue; background: white; overflow:hidden;" 
		+ " width:" + (((resourceVisualization.getWidth()*groupWidth)/1000)) + "px;"
		+ " height:" + ((resourceVisualization.getWidth()*1000)/resourceVisualization.getHeight()) + "px;"
		+ " left:" + (((resourceVisualization.getPositionX()*groupWidth)/1000)) + "px;"
		+ " top:" + (((resourceVisualization.getPositionY()*groupHeight)/1000)) + "px;\""
		+ " zIndex:" + zAxis + "\""
		+ " onmouseup=\"mouseUpOnResVisu('" + resourceVisualization.getURI().toString() + "_border', '" + resourceVisualization.getURI().toString() + "')\""
		+ " onmousedown=\"mouseDownForForeGround('" + resourceVisualization.getURI().toString()+ "')\""
		+ ">"
		//div-open URI
		+	"<div id=\"" + resourceVisualization.getURI().toString() + "\"" 
		+ " height:" + (((resourceVisualization.getHeight()*groupHeight)/1000)-8) + "px;"
		+ " style=\"min-width:130px; min-height:100px; background:white; padding:2px; cursor:default;"
		+ "\">";
		return div;
	}
	
	public String getMoveBar(ResourceVisualization resourceVisualization, String widthMove){
//		div open _move
		String movebar = "<div id=\"" + resourceVisualization.getURI().toString() + "_move\" style=\"min-width:160px;" + widthMove + "height:8px; position:absolute; bottom:0px; background:#004499; cursor:nw-resize;\""
			 + " onmouseup=\"mouseUpOnResVisu('" + resourceVisualization.getURI().toString() + "_border', '" + resourceVisualization.getURI().toString() + "')\""
			 + " onmousedown=\"resize1('" + resourceVisualization.getURI().toString() + "', event);\">";
		//close div move
		return movebar;
	}
	
	/**
	 * Returns the Head of the BigResourceView.
	 * @param resVisuID Id of the ResourceVisualization which should be displayed in big mode
	 * @param windowWidth the width of the visible browserwindow
	 * @param windowHeight the height of the visible browserwindow
	 * @return the head of the big view as a String
	 */
	public String getBigResourceVisualizationHead(ResourceVisualization resourceVisualization, Integer windowWidth, Integer windowHeight){
		String head = 	"<div id=\"bigResourceVisualization\" style=\"width:" + (windowWidth*0.5) + "px; position:absolute; left:" + (windowWidth*0.25) + "px; top:" + (windowWidth*0.03) + "px; text-align:center; vertical-align:middle; background:white;\">"
						+ 	"<!-- title image, logo:  -->"
						+ 	"<div id=\"header\" style=\"width:" + (windowWidth*0.5) + "px;\">"
						+ 		"<img src=\"" + VIEW_VOCABULARY.GROUPME_BASE_URL + "images/semweb-logo.gif\" alt=\"Semantic Web\"/>"
						+		"<div id=\"titletext\">"
						+ 			"<h1 class=\"title\">GroupMe!</h1>"
						+ 			"<p class=\"subtitle\">[structure your life]</p>"
						+		"</div>"
						+		"<p class=\"descriptionText\">Group everything!</p> "
						+ 	"</div>"
						+ 	"<!-- Menu: -->"
						+ 	"<div id=\"menuBRV\" style=\"text-align:left; width:" + (windowWidth*0.5) + "px;\">"
						+		"<div style=\"float:right; margin-right:1em;\">"
						+			"<a style=\"padding-right:1em;\" href=\"./api/" + VIEW_VOCABULARY.ACTION_EXPORT + "?" + ExportController.PARAMETER_RESOURCE_ID + "=" + resourceVisualization.getResource().getId() + "&" + ExportController.PARAMETER_MODE + "=" + ExportController.PARAMETER_MODE_RESOURCES +"&" + ExportController.PARAMETER_EXPORT_FORMAT + "=rdf\" target=\"GroupMe! RDF-Export\" title=\"Export GroupMe! Resource to RDF\">"
						+				"<img src=\"./images/g-48x48.gif\" height=\"24px\" width=\"24px\" style=\"text-decoration:none;border:none;\"/>"
						+			"</a>"
//						+			"<a href=\"./api/" + VIEW_VOCABULARY.ACTION_EXPORT + "?" + ExportController.PARAMETER_RESOURCE_ID + "=" + resourceVisualization.getResource().getId() + "&" + ExportController.PARAMETER_MODE + "=" + ExportController.PARAMETER_MODE_RESOURCES +"&" + ExportController.PARAMETER_EXPORT_FORMAT + "=rss\" target=\"GroupMe! RSS-Export\" title=\"Export GroupMe! Resource to RSS\">"
//						+				"<img src=\"./images/rss-48x48.gif\" height=\"24px\" width=\"24px\" style=\"text-decoration:none;border:none;\"/>"
//						+			"</a>"
						+		"</div>"
						+		"<div style=\"padding-left:1em;\">"
						+ 			"menu: "
						+			"[<a  id=\"detailButton\" href=\"javascript:showDetailView('" + resourceVisualization.getResource().getId() + "', '" + resourceVisualization.getId() + "')\" onfocus=\"if(this.blur)this.blur()\" title=\"show the details of this Resource\">details</a>]"
						+			"[<a  href=\"javascript:addTag('" + resourceVisualization.getResource().getId() + "', '" + resourceVisualization.getURI().toString() + "')\" onfocus=\"if(this.blur)this.blur()\" title=\"add a Tag to the Resource\">add Tag</a>]"
						+ 			"[<a  href=\"" + resourceVisualization.getResource().getURL() + "\" target=\"_blank\" title=\"Open this Resource a in new window\">open</a>]"
						+ 			"[<a  href=\"javascript:closeBigResource();\" onfocus=\"if(this.blur)this.blur()\" title=\"Close this big view\">close</a>]"
						+		"</div>"
						+ 	"</div>"
						+ 	"<!-- content -->"
						+ 	"<div id=\"mainBRV\" style=\"width:" + (windowWidth*0.5) + "px; height:" + ((windowHeight*0.95)-150) + "px; text-align:center; vertical-align:middle;\">";
		return head;
	}
	
	/* (non-Javadoc)
	 * @see net.groupme.gui.rendering.Renderer#renderAsHTML(net.groupme.model.ResourceVisualization)
	 */
	public abstract String renderAsHTML(ResourceVisualization resourceVisualization, Integer groupWidth, Integer groupHeight, Integer zAxis) ;
	
	/**
	 * Renders a list of {@link HtmlFormattedElement} instances in a way that they are separated by comma and
	 * returns a String which contains this comma-separated list.
	 * @param elements elements to combine by using a comma
	 * @return combination of comma separated String which may look like this:
	 * <pre>
	 * <a href="...">element 1</a>, <a href="...">element 2</a>, <a href="...">element 2</a>
	 * </pre>
	 */
	public static final String separateHtmlFormattedElementsByComma(HtmlFormattedElement[] elements){
		String result = "";
		if(elements != null){
			for(int i=0; i<elements.length; i++){
				result += elements[i].getHtml();
				if(i != (elements.length - 1)){
					result += ", ";
				}
			}
		}
		return result;
	}
	
	/**
	 * Renders a list of strings in a way that they are separated by comma and
	 * returns a String which contains this comma-separated list.
	 * @param elements elements to combine by using a comma
	 * @return combination of comma separated String which may look like this:
	 * <pre>
	 * <a href="...">element 1</a>, <a href="...">element 2</a>, <a href="...">element 2</a>
	 * </pre>
	 */
	public static final String separateStringsByComma(List<String> elements){
		String result = "";
		if(elements != null){
			for(int i=0; i<elements.size(); i++){
				result += elements.get(i);
				if(i != (elements.size() - 1)){
					result += ", ";
				}
			}
		}
		return result;
	}

}
